package net.hzlj.platform.enterprise.utils.recorder;

import com.czt.mp3recorder.Mp3Recorder;

import java.io.File;
import java.util.UUID;

/**
 * Created by Administrator on 2016/8/22 0022.
 */
public class Mp3RecorderAudioImpl implements RecorderAudioService {

    private static final String TAG = Mp3RecorderAudioImpl.class.getSimpleName();
    private Mp3Recorder mRecorder;
    private File mDir;// 保存录音的文件夹
    private String mCurrentFilePath;// 录音文件path
    private boolean isPrepared;// 是否已经准备完毕
    private RecorderAudioManager.AudioStateListener mListener;

    private int mMaxAmplitude;


    @Override
    public void setOnAudioStateListener(RecorderAudioManager.AudioStateListener listener) {
        this.mListener = listener;
    }

    @Override
    public void setRecorderFileDir(File dir) {
        this.mDir = dir;
    }


    @Override
    public void prepareAudio() {
        try {
            isPrepared = false;
            String fileName = generateFileName();
            File file = new File(mDir, fileName);
            mCurrentFilePath = file.getAbsolutePath();
            mRecorder = new Mp3Recorder();
            mRecorder.setOutputFile(mCurrentFilePath)
                    .setMaxDuration(10*60)
                    .setCallback(new Mp3Recorder.Callback() {
                        @Override
                        public void onStart() {

                        }

                        @Override
                        public void onPause() {

                        }

                        @Override
                        public void onResume() {

                        }

                        @Override
                        public void onStop(int i) {

                        }

                        @Override
                        public void onReset() {

                        }

                        @Override
                        public void onRecording(double v, double v1) {
                            mMaxAmplitude = (int) v1;
                        }

                        @Override
                        public void onMaxDurationReached() {

                        }
                    });
            mRecorder.start();
            isPrepared = true;// 准备结束
            if (null != mListener) {
                mListener.wellPrepared();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void release() {
        try{
            if (mRecorder != null){
                mRecorder.reset();
                mRecorder = null;
            }
        }catch (Exception e){
            e.printStackTrace();
            mRecorder = null;
        }
    }

    @Override
    public void cancel() {
        if (null != mCurrentFilePath) {
            File file = new File(mCurrentFilePath);
            file.delete();
            mCurrentFilePath = null;
        }
        release();
    }

    @Override
    public String getCurrentFilePath() {
        return mCurrentFilePath;
    }

    @Override
    public int getVoiceLevel(int maxLevel) {
//        Log.i(TAG,"getMaxVolume" + mRecorder.getMaxVolume());
//        Log.i(TAG,"getRealVolume" + mRecorder.getRealVolume());
//        Log.i(TAG,"getVolume" + mRecorder.getVolume());
//        return mRecorder.getVolume() /  (mRecorder.getMaxVolume() / maxLevel);
        int level = 1;
        if (isPrepared && null != mRecorder) {
            // 获得它最大的震幅 getMaxAmplitude()范围是1~32767
            level = maxLevel * mMaxAmplitude / 32768 + 1;
        }
        return level;
    }

    /**
     * 随机生成文件名称
     *
     * @return
     */
    private String generateFileName() {
        return UUID.randomUUID().toString() + ".mp3";
    }

}
